package com.lyfx.reptiles.utils;

import com.lyfx.common.pojo.CatPojo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 分类菜单爬虫获取
 * @author lsy
 * @create 2020-07-26-12:38
 */
public class CategoryUtil {

    public static void main(String[] args) {
        Long a1 = 229L;
        Long a2 = 229L;
        System.out.println(a1.compareTo(a2));
        System.out.println(a1==a2);
    }
    public static List<CatPojo> catPojoList() throws Exception {
        String url = "https://www.zhipin.com/";
        Document document = Jsoup.parse(new URL(url), 30000);
        Elements select = document.select(".menu-sub");
        List<CatPojo> list = new ArrayList<>();
        Long num = 0L;
        Long parId = 0L;
        Long par2Id = 0L;
        for (Element element : select) {
            num++;
            //一级分类
            String p = element.getElementsByTag("p").eq(0).text();
            CatPojo catPojo1 = new CatPojo(num,p,0L,1,0,"");
            list.add(catPojo1);
            parId = num;
            Elements lis = element.getElementsByTag("li");
            for (Element li : lis) {
                //二级分类
                num++;
                par2Id = num;
                String h4 = li.getElementsByTag("h4").text();
                CatPojo catPojo2 = new CatPojo(num,h4,parId,2,0,"");
                list.add(catPojo2);

                Elements a = li.getElementsByTag("a");
                for (Element as : a) {
                    //三级分类
                    num++;
                    String text = as.text();
                    CatPojo catPojo3 = new CatPojo(num,text,par2Id,3,0,"");
                    list.add(catPojo3);
                }
            }
        }
        return list;
    }
}
